using System.Data;
using System.Linq;
using Home.Site.Models.Account;

namespace Home.Site.Models.DataLayer
{
    public class MusicRepository : IMusicRepository
    {
        readonly PromikeDBEntities context = new PromikeDBEntities();

        public IQueryable<Music> GetMusicByUser(string userName)
        {
            return context.Musics;
        }

        public Music Find(int id)
        {
            return context.Musics.Find(id);
        }

        public void InsertOrUpdate(Music music)
        {
            if (music.idmusic == default(int))
            {
                // New entity
                context.Musics.Add(music);
            }
            else
            {
                // Existing entity
                context.Entry(music).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var music = context.Musics.Find(id);
            context.Musics.Remove(music);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public bool FindByName(string filename, UserProfile author)
        {
            return context.Musics.Any(x => x.author == author.UserName && x.filename == filename);
        }
    }
}